$(function () {
    getUrl('accounterCenter/addCard');

    var cookie = new sweetCookie();
    var bankJson = cookie.getCookie('bank');
    var bankTypeJson = cookie.getCookie('bankType');
    if (bankJson && bankJson != "\"\"" && bankTypeJson && bankTypeJson != "\"\"") {
        var bank = decodeURIComponent(bankJson);
        var bankType = decodeURIComponent(bankTypeJson);
        $('#bank').val(bank);
        $('#accountType').val(bankType);
        if (bankType == 0)
            bankType = '信用卡';
        if (bankType == 1)
            bankType = '储蓄卡';
        if (bankType.length > 1)
            $('#bankType').text(bankType);
    } else {
        alertBox('网页已过期', '');
        setTimeout('window.location.href = "/Lvscore_User_M/userWeb/views/accounterCenter/addCard.html"', 1500);
    }

    //姓名校验
    $('#accountUserName').blur(function () {
        validateName();
    });
    //证件校验
    $('#credentialsType').change(function () {
        validateCredentials();
    });
    $('#credentialsNum').blur(function () {
        validateCredentials();
    });

    //银行卡类型号码校验
    $('#accountNum').blur(function () {
        validateAccount();
    });

    //手机号码校验
    $('#bank_tel').blur(function () {
        validateTel();
    });

    //发送验证码
    $('#get').click(function () {
        var accountNum = $('#accountNum').val();
        var bankName = $('#bank').val();
        var accountType = $('#accountType').val();
        var accountUserName = $('#accountUserName').val();
        var credentialsType = $("#credentialsType").val();
        var credentialsNum = $("#credentialsNum").val();
        var tel = $('#bank_tel').val();
        //验证是否有未填项
        if (ifAllInfo(accountNum, bankName, accountType, tel, accountUserName, credentialsType, credentialsNum)) {
            if (validateName() && validateCredentials() && validateAccount() && validateTel()) {
                ajaxGetCodeToBankCardPhone(accountNum,tel,this);
            }
        } else
            alertBox('不能有未填项','');
    });

    //点击绑定银行卡按钮
    $("#binding").click(function () {
        var accountNum = $('#accountNum').val();
        var bankName = $('#bank').val();
        var accountType = $('#accountType').val();
        var accountUserName = $('#accountUserName').val();
        var credentialsType = $("#credentialsType").val();
        var credentialsNum = $("#credentialsNum").val();
        var mobel = $('#bank_tel').val();
        var code = $('#code').val();
        //验证是否有未填项
        if (ifAllInfo(accountNum, bankName, accountType, mobel, accountUserName, credentialsType, credentialsNum)) {
            if (validateName() &&validateCredentials() && validateAccount() && validateTel()) {
                if (!isNotEmpty(code))
                    alertBox('请填写验证码', '');
                else
                    ajaxValidateBankCode(code, accountNum, bankName, accountType, accountUserName, credentialsType, credentialsNum, mobel);
            }
        } else
            alertBox('不能有未填项', '');

    });


});

//校验姓名
function validateName() {
    var name = $('#accountUserName').val();
    if (isNotEmpty(name)) {
        if (isRealName(name)) {
            $("#name_validate").html('');
            return true;
        } else {
            validateSpanMsg('name_validate', '姓名格式错误');
            return false;
        }

    } else {
        validateSpanMsg('name_validate', '姓名不能为空');
        return false;
    }

}

//校验证件信息
function validateCredentials() {
    var credentialsType = $('#credentialsType').val();
    var credentialsNum = $('#credentialsNum').val();
    if (isNotEmpty(credentialsType)) {
        if (isNotEmpty(isCredentials(credentialsType, credentialsNum))) { //证件校验 返回空 则验证通过
            validateSpanMsg('credentials_validate', isCredentials(credentialsType, credentialsNum));
            return false;
        }
        else {
            validateSpanMsg('credentials_validate');
            return true;
        }
    } else {
        validateSpanMsg('credentials_validate', '证件号码不能为空');
        return false;
    }
}

//校验手机号
function validateTel() {
    var mobel = $('#bank_tel').val();
    if (isNotEmpty(mobel)) {
        if (isMobile(mobel)) {
            validateSpanMsg('tel_validate');
            return true;
        } else {
            validateSpanMsg('tel_validate', '手机号码格式错误');
            return false;
        }
    } else {
        validateSpanMsg('tel_validate', '手机号码不能为空');
        return false;
    }
}
//校验银行卡信息
function validateAccount() {
    var accountNum = $('#accountNum').val();
    if (isNotEmpty(accountNum)) {
        if (isBankCard(accountNum)) {
            var info = getBankName(accountNum);
            var infos = info.split('-');
            var bankName = infos[0];
            var bankType = infos[2];
            var bank = $('#bank').val();
            if (bank.indexOf('中国') == 0 && bank.length != 4) //排除中国银行的干扰
                bank = bank.substring(2, bank.length);
            if (bank != bankName) {
                validateSpanMsg('account_validate', '该银行卡与所选开户银行不符');
                return false;
            } else if (getBankTypeCode(bankType) != getBankTypeCode($('#bankType').text())) {
                validateSpanMsg('account_validate', '该银行卡与所选类型不符');
                return false;
            } else {
                validateSpanMsg('account_validate', '');
                return true;
            }
        } else {
            validateSpanMsg('account_validate', '银行卡号格式错误');
            return false;
        }
    } else {
        validateSpanMsg('account_validate', '银行卡号不能为空');
        return false;
    }
}
function getBankTypeCode(bankType) {
    return (bankType == '借记卡' || bankType == '储蓄卡') ? 1 : (bankType == '信用卡' || bankType == '贷记卡') ? 0 : -1;
}

function ifAllInfo(accountNum, bankName, accountType, tel, accountUserName, credentialsType, credentialsNum) {
    if (isNotEmpty(accountNum) && isNotEmpty(bankName) && isNotEmpty(accountType) && isNotEmpty(tel) &&
        isNotEmpty(accountUserName) && isNotEmpty(credentialsType) && isNotEmpty(credentialsNum))
        return true;
    return false;
}

/**
 * 由银行向用户绑定的手机发送验证码
 * @param tel_num    银行卡预留手机号码
 */
function ajaxGetCodeToBankCardPhone(accountNum, tel_num,that) {
    var url = "/Lvscore_User_M/account/ajaxGetCodeToBankCardPhone";
    $.post(
        url,
        {
            'accountNum' : accountNum,
            'tel_num': tel_num
        },
        function (data) {
            if (data == 1) {
                alertBox('发送成功');
            }else if (data == -1) {
                alertBox('发送失败','');
            }else if (data == -2) {
                alertBox('您已绑定过此银行卡', '');
            }else{
                alertBox('服务器异常', '');
            }
            settime(that);
           //alert("asd");
        },
        "json");}

/**
 * 同意协议并绑定 校验 验证码
 */
function ajaxValidateBankCode(code, accountNum, bankName, accountType, accountUserName, credentialsType, credentialsNum, mobel) {
    var url = "/Lvscore_User_M/account/ajaxValidateBankCode";
    $.post(
        url,
        {'code': code},
        function (data) {
            if (data == 1)
                ajaxaddNewBankCard_StepThree(accountNum, bankName, accountType, accountUserName, credentialsType, credentialsNum, mobel);
            else if (data == -1)
                alertBox('验证码错误', '');
            else
                alertBox('服务器异常', '');
        },
        "json"
    );
}

/**
 * 新增银行卡 执行
 * @param accountNum        卡号
 * @param bankName          开户行
 * @param accountType       银行卡类型
 * @param accountUserName   开户人姓名
 * @param credentialsType   证件类型    0：身份证 1：护照 3：户口本
 * @param credentialsNum    证件号码
 * @param mobel             银行预留手机号
 * @returns
 */
function ajaxaddNewBankCard_StepThree(accountNum, bankName, accountType, accountUserName, credentialsType, credentialsNum, mobel) {
    var url = "/Lvscore_User_M/account/ajaxaddNewBankCard_StepThree";
    $.post(
        url,
        {
            'accountNum': accountNum,
            'bankName': bankName,
            'accountType': accountType,
            'accountUserName': accountUserName,
            'credentialsType': credentialsType,
            'credentialsNum': credentialsNum,
            'mobel': mobel
        },
        function (data) {
            if (data == 1) {
                alertBox('添加成功');
                setTimeout('window.location.href = "/Lvscore_User_M/userWeb/views/accounterCenter/bindCardSucc.html"', 1500);
            }else if (data == -1) {
                alertBox('添加失败', '');
            }else{
                alertBox('服务器异常', '');
            }
        },
        "json"
    );
}

/**
 * 根据银行卡或获取发卡行
 * @param bankCard 银行卡号
 * @returns {*}
 */
var getBankName = function (bankCard) {
    var bank = "未知发卡银行";
    if (bankCard == null || bankCard == "") {
        return "";
    }
    $.ajaxSettings.async = false;
    $.getJSON("/Lvscore_User_M/userWeb/static/js/bankData/bankData.json", function (data) {
        var bankBin = 0;
        var isFind = false;
        for (var key = 10; key >= 2; key--) {
            bankBin = bankCard.substring(0, key);
            $.each(data, function (i, item) {
                if (item.bin == bankBin) {
                    isFind = true;
                    bank = item.bankName;
                }
            });
            if (isFind) {
                break;
            }
        }
    });
    $.ajaxSettings.async = true;
    return bank;
}
